// jSquare Frame Independent Global Storage
jsq.gs=function(name,deleteOnUnload) {
	var ret=jsq.gs.get(name);
	if (!ret) {
		if (jsq.gs.enableOpener && jsq.gs.opener) {
			ret=jsq.gs.opener.jsq.globalStorage.create(name);
		} else {
			if (!jsq.gs.window._jsquare_gs_storage) jsq.gs.window._jsquare_gs_storage={};
			ret=jsq.gs.window._jsquare_gs_storage[name]={};
		}
		if (ret) jsq.gs.nameCache[name]=jsq.isUndefined(deleteOnUnload)?true:deleteOnUnload;
	}
	return ret;
};

jsq.extend(jsq.gs,{
    nameCache:{},
    childs:[],
    childsAll:[],
    window:window,
    opener:null,
    enableOpener:false,

    get:function(name() {
    	if (this.enableOpener && this.opener) {
    		return jsq.gs.opener.jsq.gs.get(name);
    	}
    	if (this.window && this.window._jsquare_gs_storage) {
    		return this.window._jsquare_gs_storage[name];
    	}
    	return null;
    },
    remove:function(name,removeMine) {
    	if (removeMine==true) {
    		if (this.nameCache[name]!=true && this.nameCache[name]!=false) return;
    	}
    	if (this.enableOpener && jsq.gs.opener) {
    		this.opener.jsq.gs.remove(name);
    	} else if (this.window._jsquare_gs_storage&&jsq.gs.window._jsquare_gs_storage[name]) jsq.gs.window._jsquare_gs_storage[name]=null;
    	jsq.gs.nameCache[name]=null;
    },
    $:function(element) {
    	var tmp=jsq.selector.$(element);
    	var ret=[];
    	if (tmp) {
    		if (typeof tmp.length=='number') ret=$A(tmp);
    		else ret.push(tmp);
    	}
    	for(var i=0;i<jsq.gs.childsAll.length;i++) {
    		tmp=jsq.gs.childsAll[i].jsq.selector.$(element);
    		if (tmp) {
    			if (typeof tmp.length=='number') ret=ret.concat(jsq.$A(tmp));
    			else ret.push(tmp);
    		}
    	}
    	return ret;
    }
});

jsq.gs.onUnload=function() {
	if (jsq.gs.nameCache) {
		for(selector in jsq.gs.nameCache) {
			if (jsq.gs.nameCache[selector]==true) {
				jsq.gs.window._jsquare_gs_storage[name]=null;
			}
		}
	}
	var win=window;
	for(var depth=0;win.parent;depth++,win=win.parent) {
		try {
			if (!win.parent.document || !win.parent.document.body || jsq.isUndefined(win.parent.swaf)) break;
			if (win.parent.document.body.tagName!='BODY') break;
			if (window.top==win) break;
		} catch(e) { break;}
		if (depth==0) {
			win.parent.jsq.gs.childs.removeCompact(win);
		}
		win.parent.jsq.gs.childsAll.removeCompact(win);
	}
};
jsq.gs.onLoad=function() {
	var win=window;
	for(var depth=0;win.parent;depth++,win=win.parent) {
		try {
			if (!win.parent.document || !win.parent.document.body || typeof win.parent.swaf=='undefined') break;
			if (win.parent.document.body.tagName!='BODY') break;
			if (window.top==win) break;
		} catch(e) { break;}
		if (depth==0) {
			win.parent.jsq.gs.childs.push(win);
		}
		win.parent.jsq.gs.childsAll.push(win);
	}
	jsq.gs.window=win;
	try {
		if (win.opener && win.opener.swaf) jsq.gs.opener=win.opener;
	} catch(e) {
	  jsq.gs.opener=null;
	}
};
jsq.gs.getAllWindows=function() {
	return jsq.gs.window.jsq.gs.childsAll;
};

var _global_onLoad=function() {
// init global storage
	jsq.gs.onLoad();
// call init function
	jsq.onLoadDocument();
};
var _global_onUnLoad=function() {
	jsq.onUnloadDocument();
// resource clean
	if (jsq.browser.isIE) {
		jsq._event.resetListener();
		jsq.message.resetListener();
	}
	jsq.gs.onUnload();
};

if (!window.$gs) window.$gs=jsq.gs;

jsq.listener(window,"load",_global_onLoad);
jsq.listener(window,"unload",_global_onUnLoad);
